<?php
namespace Home\Controller\Mobile;
use Think\Controller;

class LoginController extends Controller {
     
    public function login(){
    	$par['flag'] = 'system';
		$system = M('sxkj_offline_data')->where($par)->find();
		$system = json_decode($system['data'],true);

		$jsxcode = I("request.jsxcode");

		$this->assign('jsxcode',$jsxcode);
    	$this->assign('system',$system);
        $this->display();
    }
    
    //验证码发送
    public function sendsms(){
    	$mobile = I('request.phone');
    	$flag = I('request.flag');    		
    	$code = mt_rand(100000,999999);

    	$u_par['phone'] = $mobile;
    	$user_info = M('sxkj_offline_user')->where($u_par)->field('id')->find();
	   	if(empty($flag) && empty($user_info)){
	   		echo '该手机还未注册，先注册吧~';exit;
	   	}
 		if($flag == 'register' && !empty($user_info)){
			echo '该手机账户已存在，去登录吧~';exit;
	   	}
    	$params = $mobile.','.$code;
    	
   		$par['flag'] = 'system';
		$system = M('sxkj_offline_data')->where($par)->find();
		$system = json_decode($system['data'],true);
        					
		if(empty($system['msg'])){
			echo '短信发送失败';exit;	
		}
		
		$url = 'https://dxapi.xintm.cn/api/sms_send.html';
	    $account = $system['code_api_user'];
	    $password = $system['code_api_pwd'];
	    $msg = $system['msg'];
	    try {
	        $data['account'] = $account;
	        $data['password'] = $password;
	        $data['msg'] = $msg;
	        $data['params'] = $params;
	        $ch = curl_init();
		    curl_setopt($ch, CURLOPT_URL, $url);
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		    // post数据
		    curl_setopt($ch, CURLOPT_POST, 1);
		    // post的变量
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		    //不验证证书下同
		    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		    if(!empty($headers)){
		        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
		    }
		    $output = curl_exec($ch);
		    curl_close($ch);
		    $res = json_decode($output,true);
	        if($res['code']==0){
	        	session('code',$code);
	        	session('phone',$mobile);
	        	echo '1';exit;
	        }else{
	        	echo $res['errorMsg'];exit;			
	        }
	        //file_put_contents("test.txt",$res);
	    } catch (ClientException $e) {
	        //echo $e->getErrorMessage() . PHP_EOL;
	    } catch (ServerException $e) {
	        //echo $e->getErrorMessage() . PHP_EOL;
	    } 
    }
    
    //登录
    public function dologin(){
    	$sess_code = session('code');
    	$code = I('post.code');
    	$mobile = I('post.phone');
    	$password = I('post.password');
    	
    	if($code != $sess_code){
			echo '验证码输入错误！';exit;
		} 
    	
    	$u_par['phone'] = $mobile;
    	$user_info = M('sxkj_offline_user')->where($u_par)->field('id,phone')->find();
   		if(empty($user_info)){
	   		echo '该手机还未注册，先注册吧~';exit;
	   	}
    		  		
		$userarr = array(
			'uid'=>$user_info['id']
		);
		session('user',$userarr);
		echo '1';exit;   	  	
    }
       
    public function register(){
		$par['flag'] = 'system';
		$system = M('sxkj_offline_data')->where($par)->find();
		$system = json_decode($system['data'],true);

		$jsxcode = I("request.jsxcode");

		$this->assign('jsxcode',$jsxcode);
    	$this->assign('system',$system);
        $this->display();
    }
    
    //注册
    public function doregister(){
    	$sess_code = session('code');
    	$phone = session('phone');
    	$code = I('post.code');
    	$mobile = I('post.phone');

    	$u_par['phone'] = $mobile;
    	$user_info = M('sxkj_offline_user')->where($u_par)->field('id')->find();
     
		if($code != $sess_code){
			echo '验证码输入错误！';exit;
		}
		if($mobile!=$phone){
			//echo '前后手机号码不一致！';exit;
		}
		if($user_info['id']){
			echo '该手机账户已存在，去登录吧~';exit;
		}else{
			$arr = array(
				'nickname' =>I('post.nickname'),
				'phone'=>$mobile,
				'add_time'=>time(),
			);	
			//上级id
			$jsxcode = I('post.jsxcode');
            if(!empty($jsxcode)){
                $par_sup['jsxcode'] = $jsxcode;
                $sup_uid = M('sxkj_offline_user')->where($par_sup)->getField('id');
                if(!empty($sup_uid)){
                    $arr['sup_promoterid'] = $sup_uid;
                }
            }
			$result = M('sxkj_offline_user')->data($arr)->add();
			if($result){
				echo '1';exit;
			}else{
				echo '2';exit;
			}
		}	
    }
    
    public function get_http($partnerid){   		
		$par_system['flag'] = 'system';
		$par_system['partnerid'] = $partnerid;
    	$sys = M('jq_hotel_data')->where($par_system)->find(); 
	    $system = json_decode($sys['data'],true); 
		if($system['agreement']==1||empty($system['agreement'])){
			if($_SERVER['HTTPS'] === 1){  //Apache  
				$h = 'https';  
			}elseif($_SERVER['HTTPS'] === 'on'){ //IIS  
				$h = 'https'; 
			}elseif($_SERVER['SERVER_PORT'] == 443){ //其他  
				$h = 'https'; 
			}else{
				$h = 'http';
			}
		}elseif($system['agreement']==2){
			$h = 'http';
		}elseif($system['agreement']==3){
			$h = 'https';
		}
		return $h;		
   	}
  
}